package com.deeper.customview.ui.c4.exercise.c04e02;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

/**
 * 将任一图片显示为图所示形状
 */
public class ShaderView extends View {

    private int width;
    private int height;
    private int radius;
    private int centerX;
    private int centerY;

    private int triangleRadius;
    private PathEffect pathEffect;
    private Bitmap maskBitmap;
    private Bitmap maskDstBitmap;
    private Bitmap maskSrcBitmap;
    private Canvas maskCanvas;
    private Canvas maskDstCanvas;
    private Canvas maskSrcCanvas;
    private Paint maskPaint;
    private Paint maskDstPaint;
    private Paint maskSrcPaint;
    private Bitmap bmpImage;
    private Paint finalPaint;
    private Bitmap bmpFinal;


    public ShaderView(Context context) {
        super(context);
    }


    public ShaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    public ShaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getMeasuredHeight();

        int maxLength = Math.min(width, height);
        radius = maxLength / 2 - 50;
        triangleRadius = radius / 4;
        centerX = maxLength / 2;
        centerY = maxLength / 2;

        pathEffect = new CornerPathEffect(triangleRadius / 4f);


        bmpImage = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
        initMaskBmp(maxLength);
        bmpFinal = Bitmap.createBitmap(maxLength, maxLength, Bitmap.Config.ARGB_8888);

        finalPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }


    private void initMaskBmp(int maxLength) {
        maskBitmap = Bitmap.createBitmap(maxLength, maxLength, Bitmap.Config.ARGB_8888);
        maskDstBitmap = Bitmap.createBitmap(maxLength, maxLength, Bitmap.Config.ARGB_8888);
        maskSrcBitmap = maskDstBitmap.copy(Bitmap.Config.ARGB_8888, true);

        maskCanvas = new Canvas(maskBitmap);
        maskDstCanvas = new Canvas(maskDstBitmap);
        maskSrcCanvas = new Canvas(maskSrcBitmap);

        maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        maskDstPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        maskSrcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawMaskBmp();

        int layer = canvas.saveLayer(new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius), null);
//        canvas.drawBitmap(bmpImage, 0, 0, finalPaint);
        //图片不够大，所以用此矩阵放大
        Matrix matrix = new Matrix();
        matrix.setScale(1f, 2f);
        canvas.drawBitmap(bmpImage, matrix, finalPaint);
        finalPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(maskBitmap, 0, 0, finalPaint);
        finalPaint.setXfermode(null);
        canvas.restoreToCount(layer);
        canvas.drawBitmap(bmpFinal, 0, 0, null);
    }


    private void drawMaskBmp() {
        //绘制遮罩圆角方形
        maskDstPaint.reset();
        maskDstPaint.setColor(Color.BLACK);
        maskDstPaint.setStyle(Paint.Style.FILL);
        maskDstCanvas.drawRoundRect(centerX - radius, centerY - radius, centerX + radius, centerY + radius, 100, 100, maskDstPaint);

        //绘制遮罩圆角等边三角形
        maskSrcPaint.reset();
        maskSrcPaint.setColor(Color.GREEN);
        maskSrcPaint.setStyle(Paint.Style.FILL);
        maskSrcPaint.setPathEffect(pathEffect);

        Path trianglePath = new Path();
        trianglePath.moveTo(0, 0);
        trianglePath.lineTo((float) (triangleRadius * Math.sqrt(3) / 2f), 3f * triangleRadius / 2);
        trianglePath.lineTo((float) (triangleRadius * Math.sqrt(3) / 6f), 3f * triangleRadius / 2);
        trianglePath.lineTo((float) (triangleRadius * Math.sqrt(3) / 6f), 2.5f * triangleRadius);
        trianglePath.lineTo(-(float) (triangleRadius * Math.sqrt(3) / 6f), 2.5f * triangleRadius);
        trianglePath.lineTo(-(float) (triangleRadius * Math.sqrt(3) / 6f), 3f * triangleRadius / 2);
        trianglePath.lineTo(-(float) (triangleRadius * Math.sqrt(3) / 2f), 3f * triangleRadius / 2);
        trianglePath.close();

        maskSrcCanvas.translate(centerX, 50);
        maskSrcCanvas.rotate(45, 0, radius);
        maskSrcCanvas.drawPath(trianglePath, maskSrcPaint);

        maskSrcCanvas.rotate(90, 0, radius);
        maskSrcCanvas.drawPath(trianglePath, maskSrcPaint);

        maskSrcCanvas.rotate(90, 0, radius);
        maskSrcCanvas.drawPath(trianglePath, maskSrcPaint);

        maskSrcCanvas.rotate(90, 0, radius);
        maskSrcCanvas.drawPath(trianglePath, maskSrcPaint);

        //绘制遮罩
        int layer = maskCanvas.saveLayer(new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius), null);
        maskCanvas.drawBitmap(maskDstBitmap, 0, 0, null);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
        maskCanvas.drawBitmap(maskSrcBitmap, 0, 0, maskPaint);
        maskPaint.setXfermode(null);
        maskCanvas.restoreToCount(layer);
        maskCanvas.drawBitmap(maskBitmap, 0, 0, null);
    }
}
